/**
 * @file bsp_mpu6050_port.h
 * @author liufeng (liufeng@injoinic.com)
 * @brief bsp mpu6050 port function.
 * @version 0.1
 * @date 2024-11-18
 *
 * @copyright Copyright (c) 2024  injoinic
 *
 */

#ifndef __BSP_MPU6050_PORT_H__
#define __BSP_MPU6050_PORT_H__

#include "stdint.h"

/* 操作系统接口 ------------------------------------------------------------------- */

#define OS FreeRTOS

// clang-format off
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
// clang-format on

// 创建互斥锁
static inline void* os_semaphore_create_mutex()
{
    return xSemaphoreCreateMutex();
}

// 非阻塞延时
static inline void os_delay(uint32_t xTicks)
{
    vTaskDelay(xTicks);
}

// 发送任务通知
static inline void os_task_notify_isr(void* xTaskToNotify, uint32_t* pxHigherPriorityTaskWoken)
{
    vTaskNotifyGiveFromISR(xTaskToNotify, (BaseType_t*)pxHigherPriorityTaskWoken);
}

// 创建队列
static inline void* os_queue_create(uint32_t item_num, uint32_t item_size)
{
    return xQueueCreate(item_num, item_size);
}

// // 获取系统时基
// static inline uint32_t os_get_tickcount()
// {
//     return xTaskGetTickCount();
// }

// // 创建二值信号量
// static inline uint32_t os_semaphore_create_binary(void** binary_semphr_handle)
// {
//     *binary_semphr_handle = xSemaphoreCreateBinary();
//     return (*binary_semphr_handle != NULL) ? 0 : 1;
// }

// // 释放信号量
// static inline uint32_t os_semaphore_give(void* semphr_handle)
// {
//     return 0;
// }

// // 队列发送
// static inline uint32_t os_queue_send(void* queue_handle, void* pdata, uint32_t timeout)
// {
//     uint32_t xReturn = xQueueSend(queue_handle, pdata, timeout);
//     return (xReturn == pdTRUE) ? 0 : 1;
// }

// // 队列接收
// static inline uint32_t os_queue_receive(void* queue_handle, void* pdata, uint32_t timeout)
// {
//     uint32_t xReturn = xQueueReceive(queue_handle, pdata, timeout);
//     return (xReturn == pdTRUE) ? 0 : 1;
// }

/* MCU函数接口 ------------------------------------------------------------------ */

void mpu6050_periph_init();
void mpu6050_periph_deinit();
uint32_t mpu6050_iic_read_byte(
    void* hi2c, uint16_t SlaveAddr, uint16_t MemAddress, uint8_t* data, uint32_t Timeout
);
uint32_t mpu6050_iic_write_byte(
    void* hi2c, uint16_t SlaveAddr, uint16_t MemAddress, uint8_t data, uint32_t Timeout
);
uint32_t mpu6050_iic_mem_read(
    void* hi2c,
    uint16_t SlaveAddr,
    uint16_t MemAddress,
    uint8_t* pData,
    uint16_t Size,
    uint32_t Timeout
);
uint32_t mpu6050_iic_mem_read_dma(
    void* hi2c, uint16_t SlaveAddr, uint16_t MemAddress, uint8_t* pData, uint16_t Size
);

#endif /* __BSP_MPU6050_PORT_H__ */
